package cn.zcy.springboot_mybatis.config;

import javax.servlet.Filter;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

//@Configuration
//@ConditionalOnProperty("spring.datasource.type")
public class DriudConfiguration {


    @Bean
    /**
     *  会绑定application.yml 所有 spring.datasource开头的配配绑定到DataSource上
     */
    @ConfigurationProperties("spring.datasource")
    public DataSource getDataSources(DataSourceProperties dataSourceProperties) {
        DataSource dataSource = dataSourceProperties.initializeDataSourceBuilder().build();
        return dataSource;
    }

    /**
     * 监控台的Servlet
     * @return
     */
    @Bean
    public ServletRegistrationBean statViewServlet(){
        ServletRegistrationBean<StatViewServlet> servletRegistrationBean =
                new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");


/*addInitParameter ===
*                   <init-param>
                        <param-name>loginUsername</param-name>
                        <param-value>admin</param-value>
                    </init-param>
* */
        // 添加IP白名单
        servletRegistrationBean.addInitParameter("allow", "127.0.0.1"); // 允许访问的IP
        servletRegistrationBean.addInitParameter("deny", "192.168.1.100"); // 拒绝访问的IP（可选）

        // 配置管理用户
        servletRegistrationBean.addInitParameter("loginUsername", "admin"); // 用户名
        servletRegistrationBean.addInitParameter("loginPassword", "admin"); // 密码

        // 是否能够重置数据
        servletRegistrationBean.addInitParameter("resetEnable", "false"); // 禁用“Reset All”功能

        return servletRegistrationBean;
    }


    /**
     * 配置服务过滤器
     * @return 返回过滤器配置对象
     */
    @Bean
    public FilterRegistrationBean<WebStatFilter> webStatFilter() {
        FilterRegistrationBean<WebStatFilter> filterRegistrationBean = new FilterRegistrationBean<>();

        WebStatFilter webStatFilter = new WebStatFilter();
        filterRegistrationBean.setFilter(webStatFilter);
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.css,/druid/*");

        return filterRegistrationBean;
    }

//    @Bean
//    public FilterRegistrationBean<StatFilter> statFilter() {
//        FilterRegistrationBean<StatFilter> filterRegistrationBean = new FilterRegistrationBean<>();
//        StatFilter statFilter = new StatFilter();
//
//        // 启用 SQL 分析
//        statFilter.setProfileEnable(true);
//
//        filterRegistrationBean.setFilter(statFilter);
//        filterRegistrationBean.addUrlPatterns("/*"); // 设置过滤的 URL 模式
//
//        return filterRegistrationBean;
//    }
}
